package com.itheima.servlet;

import com.itheima.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @Author: 祺诺
 * @Name; xqf
 * @Date: 2021/1/3 21:04
 */
@WebServlet("/ListStudentServlet")
public class ListStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader br = new BufferedReader(new FileReader("d:\\stu.txt"));
        ArrayList<Student> list = new ArrayList<>();
        String line;
        while ((line=br.readLine())!=null){
            String[] split = line.split(",");
            list.add(new Student(split[0],Integer.parseInt(split[1]),Integer.parseInt(split[2])));
        }
        br.close();
        req.setAttribute("list",list);
        req.getRequestDispatcher("/listStudent.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
